<?php

namespace App\Model\Admin;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'category';
    protected $primaryKey = 'cate_id';
    public $timestamps = false;
    protected $guarded = [];

    public function tree(){
    	$categorys = $this->orderBy('cate_order','asc')->get();
    	$data = $this->getTree($categorys);
    	return $data;
    }

    // 无限极分类
    private function getTree($data,$pid = 0){
    	static $arr = array();
    	foreach ($data as $key => $value) {
    		if($pid == $value->cate_pid){
    			$arr[] = $data[$key];
    			$this->getTree($data,$value->cate_id);
    		}
    	}
    	return $arr;
    }
}
